
//
//  SongListDetailViewController.m
//  DayByDay
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SongListDetailViewController.h"
#import "SongListDetailTableViewCell.h"
#import "GestureRecTableView.h"
#import "PosterDetailViewController.h"
#import "MVDetailsViewController.h"

@interface SongListDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, assign)CGFloat currentOffset_y;
@property (nonatomic, assign)BOOL  scrollEnable;
@property (nonatomic, strong)NSMutableArray *btnArray;
@property (nonatomic, strong)NSArray *titleArray;
@property (nonatomic, assign)NSInteger currentPage;
@property (nonatomic, assign)CGPoint point;


@end

@implementation SongListDetailViewController

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    NSLog(@"_________klsjfkdsl");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.btnArray = [NSMutableArray array];
    [self createView];
    self.scrollEnable = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeScrollEnable:) name:@"bingScrollEnable" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushVC:) name:@"PUSH" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushMVVC:) name:@"MVValue" object:nil];
    
    self.navigationController.navigationBar.alpha = 0.01;
//    self.titleArray = @[@"单曲", @"专辑", @"MV", @"歌手信息"];
}

- (void)pushMVVC:(NSNotification *)noti {
    
    MVDetailsViewController *MVVC = [MVDetailsViewController new];
//    NSMutableArray *array = [noti object];
//    NSLog(@"++++++++++++++++++++++++++++%@",array);
//    MVVC.model = [array lastObject];
    MVVC.array = [NSMutableArray arrayWithArray:[noti object]];
//    MVVC.array = array;
//    NSLog(@"%@",array);
    
    [self.navigationController pushViewController:MVVC animated:YES];
}

- (void)pushVC:(NSNotification *)noti {
    PosterDetailViewController *posterVC = [[PosterDetailViewController alloc] init];
    posterVC.value = [noti object];
    [self.navigationController pushViewController:posterVC animated:YES];
    NSLog(@"sakdfkdsf");
}

- (void)changeScrollEnable:(NSNotification *)noti {
    
    self.scrollEnable = ![[noti object] boolValue];
}

- (void)createView {
    self.tableView = [[GestureRecTableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[SongListDetailTableViewCell class] forCellReuseIdentifier:NSStringFromClass([SongListDetailTableViewCell class])];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 3)];
    imageView.backgroundColor = [UIColor grayColor];
    imageView.image = [UIImage imageNamed:@"TTFansClub_xingqiu"];
    
    self.tableView.tableHeaderView = imageView;
    NSLog(@"%f, %f",self.tableView.frame.origin.x, self.tableView.frame.origin.y);
//    AppDelegate *tempDelegate = [[UIApplication sharedApplication] delegate];
//    [tempDelegate.JHJTabBar hiddenTabBarWithAnimation:NO];
//    self.scrollEnable = YES;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getCurrtenPage:) name:@"JHJCurrentPage" object:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return HEIGHT - 64;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SongListDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SongListDetailTableViewCell class])];
    NSLog(@"===========1111111111111111");
    JHJViewController *VC = [JHJViewController new];
    [self addChildViewController:VC];
    [cell.contentView addSubview:VC.view];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"deliverValue" object:self.value];
        return cell;
    
}

-(void)dealloc{

   
        /*
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeScrollEnable:) name:@"bingScrollEnable" object:nil];
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushVC:) name:@"PUSH" object:nil];
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushMVVC:) name:@"MVValue" object:nil];
         
         */
    
    
        NSLog(@"sdfdsafdsfds f00000000007777777");
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"bingScrollEnable" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"PUSH" object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"MVValue" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"JHJCurrentPage" object:nil];

    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [UIView new];
    for (int i = 0; i < 4; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(WIDTH / 4 * i, 0, WIDTH / 4, 40);
        [view addSubview:button];
        button.tag = 40000 + i;
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitle:self.titleArray[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        button.backgroundColor = self.navigationController.navigationBar.barTintColor;
        [self.btnArray addObject:button];
    }
    UIButton *button = [self.view viewWithTag:40000];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    return view;
}

- (void)getCurrtenPage:(NSNotification *)noti {
//    NSLog(@"isfs");
    NSInteger page = [[noti object] integerValue];
//    NSLog(@"%ld",page);
//    NSLog(@"%ld",self.currentPage);
    if (page != self.currentPage) {
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"deliverValue" object:self.value];
//        NSLog(@"sjkhfjkdsjfsdklfjs");
        self.currentPage = page;
        for (UIButton *button in self.btnArray) {
            if (button.tag == self.currentPage + 40000) {
                [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            } else {
                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            }
        }
    }
}

- (void)buttonAction:(UIButton *)button {
    self.currentPage = button.tag - 40000;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"currentPage" object:[NSNumber numberWithInteger:self.currentPage]];
    for (UIButton *button1 in self.btnArray) {
        if (button1.tag == button.tag) {
            [button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        } else {
            [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}




- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"ajksfhd    ===";
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    NSLog(@"%ld",self.scrollEnable);
//    NSLog(@"%f",scrollView.contentOffset.y);
    if (scrollView.contentOffset.y > 0 && scrollView.contentOffset.y <= HEIGHT / 3 - 64) {
        self.navigationController.navigationBar.alpha = scrollView.contentOffset.y / (HEIGHT / 3 - 64) + 0.01;
        self.point = scrollView.contentOffset;
//        NSLog(@"%f",self.navigationController.navigationBar.alpha)
//        NSLog(@"%f",scrollView.contentOffset.y / (HEIGHT / 3 - 64));
    }
    
    if (scrollView.contentOffset.y < HEIGHT / 3 - 64 && self.scrollEnable) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"firstScrollEnable" object:[NSNumber numberWithBool:self.scrollEnable]];
        
    } else {
        self.scrollEnable = NO;
        scrollView.contentOffset = CGPointMake(0, HEIGHT / 3 - 64);
//        scrollView.contentOffset = self.point;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"firstScrollEnable" object:[NSNumber numberWithBool:self.scrollEnable]];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
